import { requestApi } from '../../api';
import { formatImage } from '../../utils/formatImage';
import { formatPrice } from '../../utils/util';
const app = getApp();

Page({
    data: {
        lists: [],
        startX: 0,
        startY: 0,
        isLogin: false,
        channel: 'wx_xcx',
        start: 0,
        count: 20,
        isMoreEnd: false, // 加载更多
    },
    onLoad: function (options) {
        const userinfo = wx.getStorageSync('userinfo') || {};
        let channel = options.channel || 'wx_xcx';

        if (userinfo.mobile_phone) {
            // this.getWishList(0);
            this.setData({
                channel,
            })
        }
    },
    onShow: function () {
        const userinfo = wx.getStorageSync('userinfo') || {};
        const { lists } = this.data;

        if (!userinfo.mobile_phone) {
            this.setData({
                isLogin: false
            })
        } else {
            if (lists.length < 1) {
                this.getWishList(0);
            }
            this.setData({
                isLogin: true
            })
        }
    },
    loginHandler() {
        wx.navigateTo({
            url: '/pages/account/login/login'
        })
    },
    getWishList(start) {
        let { count, lists, isMoreEnd } = this.data;
        wx.showLoading({
          title: '加载中...',
        });

        app.request({
            url: requestApi.wishList,
            data: {
                start,
                count,
                is_new_ver: true
            }
        }, (d) => {
            if (d.original.ret_code == 200) {
                const list = d.original.data.item_list.filter(item => {
                    if (item.type == 273) {
                        item.data.cover_image.full_path = formatImage.cdnImage(item.data.cover_image.path, formatImage.THUMBNAIL_M);
                        if (item.data.tax_price) {
                            item.data.tax_price_txt = formatPrice(item.data.tax_price / 100);
                        }
                        item.data.goods_price_txt = formatPrice(item.data.goods_price / 100);
                        item.data.original_price_txt = formatPrice(item.data.original_price / 100);
                        item.gid = item.data.gid
                        return item;
                    }
                });

                if (start == 0) {
                    lists = list
                } else {
                    lists = lists.concat(list)
                }

                if (lists.length >= d.original.data.total) {
                    isMoreEnd = !isMoreEnd
                }

                this.setData({
                    lists,
                    isMoreEnd
                });
            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        }, () => {
            wx.hideLoading();
            wx.stopPullDownRefresh();
        })
    },
    jumpGoods(e) {
        const { channel } = this.data;
        const gid = e.currentTarget.dataset.gid;

        wx.navigateTo({
            url: '../goods/goodsDetail/goodsDetail?gid=' + gid + '&channel=' + channel
        })
    },
    findSame(e) {
        const { channel } = this.data;
        const { brand_name, final_level_type_id } = e.currentTarget.dataset.info;

        wx.navigateTo({
            url: '/pages/categoryHall/categoryHall?keywords=' + brand_name + '&goods_type_id=' + final_level_type_id + '&channel=' + channel
        })
    },
    // 手指触摸开始事件，记录开始的(X,Y)位置
    touchStart: function (e) {
        const { lists } = this.data;
        let currentIndex = e.currentTarget.dataset.index //当前索引

        // 开始触摸时,将 isTouchMove 为 true 的置为 false，开始新的操作
        lists.forEach(function (item, index) {
            if (index != currentIndex) {
                item.isTouchMove = false;
            }
        })

        // 更新数据，记录开始的位置
        this.setData({
            startX: e.changedTouches[0].clientX,
            startY: e.changedTouches[0].clientY,
            lists
        })
    },
    // 手指滑动事件
    touchMove: function (e) {
        // 定义变量进行数据存储
        const { lists, startX, startY } = this.data;
        let currentIndex = e.currentTarget.dataset.index //当前索引
        let touchMoveX = e.changedTouches[0].clientX // 滑动变化X坐标
        let touchMoveY = e.changedTouches[0].clientY//滑动变化坐标

        //获取滑动角度
        let angle = this.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
        lists.forEach((item, index) => {
            item.isTouchMove = false;
            //滑动超过30度角 return
            if (Math.abs(angle) > 30) return;
            // 判断当前点击的是哪一个，进行操作
            if (index == currentIndex) {
                if (touchMoveX > startX) {
                     // 滑动位置大于开始位置 => 右滑
                    item.isTouchMove = false
                } else {
                     // => 左滑
                    item.isTouchMove = true
                }
            }
        })

        //更新数据
        this.setData({
            lists
        })
    },
      /**
     * 计算滑动角度
     * @param {Object} start 起点坐标
     * @param {Object} end 终点坐标
     */
    angle (start, end) {
        var _X = end.X - start.X,
            _Y = end.Y - start.Y;
        //返回角度 /Math.atan()返回数字的反正切值
        return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
    },
    handleDeleteProduct (e) {
        const gid = e.currentTarget.dataset.gid;
        let { lists } = this.data;
        let index = lists.findIndex(item => item.gid === gid);
        let userinfo = wx.getStorageSync('userinfo') || {};

        if (index >= 0) {
            wx.showLoading({
                title: '删除中...',
            });
    
            app.request({
                url: requestApi.delWish,
                method: "POST",
                data: {
                    gid: gid,
                    uid: userinfo.uid,
                    operate_type: 0,
                    is_new_ver: true
                }
            }, (d) => {
                if (d.original.ret_code == 200) {
                    lists.splice(index, 1);
                    this.setData({
                        lists
                    });
                }
            }, (error) => {
                wx.showModal({
                    title: '提示',
                    showCancel: false,
                    content: error,
                })
            }, () => {
                wx.hideLoading();
            })
        }
    },
    onPullDownRefresh() {
        this.setData({
            lists: [],
            isMoreEnd: false
        })
        this.getWishList(0);
    },
    onReachBottom: function () {
        const { lists, isMoreEnd } = this.data;
        
        if (!isMoreEnd) {
            this.getWishList(lists.length);
        }
    },
    goHome() {
        wx.switchTab({
          url: '/pages/index/index',
        })
    }
})